package com.seaside.park.controller.exhibition;

import com.alibaba.fastjson.JSONArray;
import com.seaside.common.bean.Result;
import com.seaside.park.bean.Appointment;
import com.seaside.park.mapper.AppointmentMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;

@Controller
@RequestMapping("/exhibition")
@Slf4j
public class ExhibitionController {

    @Autowired
    private AppointmentMapper appointmentMapper;

    @RequestMapping("/appointment")
    public String appointment() {
        return "exhibition/appointment";
    }

    @RequestMapping("/saveAppointment")
    @ResponseBody
    public Result<Boolean> saveAppointment(HttpServletRequest request) {
        Result result;
        try {
            String appointmentPlace = String.valueOf(request.getParameter("appointmentPlace"));
            String appointmentName = String.valueOf(request.getParameter("appointmentName"));
            String appointmentTel = String.valueOf(request.getParameter("appointmentTel"));
            //String appointmentTime = String.valueOf(request.getParameter("appointmentTime"));

            Appointment appointment = new Appointment();
            appointment.setAppointmentName(appointmentName);
            appointment.setAppointmentTel(appointmentTel);
            appointment.setAppointmentPlace(appointmentPlace);
            //appointment.setAppoinmentTime(Date.valueOf(appointmentTime));

            int ret = appointmentMapper.insert(appointment);
            if(ret>0)
            {
                result = Result.OK("预约成功！我们工作人员会和您第一时间联系!",true);
            }else{
                result = Result.Error("预约失败!");
            }
        }catch (Exception e) {
            e.printStackTrace();
            log.error(e.getMessage());
            result = Result.Error("预约失败!请重试!" + e.getMessage());
        }
        return result;
    }

    @RequestMapping("/index")
    public String index() {
        return "exhibition/index";
    }

    @RequestMapping("/test")
    @ResponseBody
    public String test(String place) {
        String port="";
        JSONArray jsonArray = new JSONArray();
        jsonArray.add("");
        jsonArray.add("云A865MD");
        jsonArray.add("");

        Map<String,Object> params = new HashMap<String,Object>();
        if(place.equals("郁金苑"))
        {
            params.put("ip","192.168.1.18");
            port = "48082";
        }else if(place.equals("芙蓉苑")){
            port = "58082";
            params.put("ip","192.168.1.7");
        }
        else if(place.equals("金盏苑")){
            port = "28082";
            params.put("ip","192.168.1.17");
        }
        else if(place.equals("紫宸苑")){
            port = "38082";
            params.put("ip","192.168.1.6");
        }
        params.put("data",jsonArray);
//        String test = HttpUtil.doPostBody("http://120.48.82.163:" + port + "/postInfo",params);
        return "处理完成";
    }

    @RequestMapping("/reset")
    @ResponseBody
    public String reset(String place) {
        String port="";
        JSONArray jsonArray = new JSONArray();
        jsonArray.add("");
        jsonArray.add("");
        jsonArray.add("");

        Map<String,Object> params = new HashMap<String,Object>();
        if(place.equals("郁金苑"))
        {
            params.put("ip","192.168.1.18");
            port = "48082";
        }else if(place.equals("芙蓉苑")){
            port = "58082";
            params.put("ip","192.168.1.7");
        }
        else if(place.equals("金盏苑")){
            port = "28082";
            params.put("ip","192.168.1.17");
        }
        else if(place.equals("紫宸苑")){
            port = "38082";
            params.put("ip","192.168.1.6");
        }
        params.put("data",jsonArray);
//        String test = HttpUtil.doPostBody("http://120.48.82.163:" + port + "/postInfo",params);
        return "处理完成";
    }

}
